package com.czd.support.util;

import org.joda.time.LocalDate;
import org.joda.time.Seconds;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * Author: Jerry.hu
 * Create: Jerry.hu (2014/8/25 23:52)
 * Description: cookie 工具类
 */
public class SessionUtils {

	/**
	 * 设置session
	 * @param request 客户端请求
	 * @param key  session
	 *  
	 */
	public static void addSession(HttpServletRequest request, String key) {
		HttpSession session = request.getSession(false);
		session.setAttribute(key, new LocalDate());
	}

	/**
	 * 判断是否存在session
	 * @param request
	 * @param key
	 * @return
	 */
	public static boolean existsSession(HttpServletRequest request, String key) {
		HttpSession session = request.getSession(false);
		return session != null && session.getAttribute(key) != null;
	}

	/**
	 * 判断session 里面的值是否超过过期时间
	 * @param request
	 * @param key
	 * @param ExpirationTime
	 * @return
	 */
	public static boolean sessionIsLate(HttpServletRequest request, String key, int ExpirationTime) {
		HttpSession session = request.getSession(false);
		if (session != null) {
			LocalDate start = (LocalDate) session.getAttribute(key);
			LocalDate end = new LocalDate();
			session.removeAttribute(key);
			return Seconds.secondsBetween(end, start).getSeconds() > ExpirationTime;
		}
		return false;
	}
}
